﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Item_Receive : System.Web.UI.Page
    {
        IBLLTree bllTree = new TreeBLL();
        IBLLSysYearType bllYearType = new SysYearTypeBLL();
        IBLLMasterPurchaseType bllPurchaseType = new MasterPurchaseTypeBLL();
        IBLLSysRecordStatus bllStatus = new SysRecordStatusBLL();
        IBLLSysProgress bllProgress = new SysProgressBLL();
        IBLLPrePurchaseRequest bllPPR = new PrePurchaseRequestBLL();
        SysControlBLL bllSysCon = new SysControlBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
                LoadYearType();
                LoadMMType();
                LoadRecordStatus();
                LoadJobSiteTree();
                LoadProgress();
                LoadDataGrid();
            }
        }

        protected void LoadDefault()
        {
            SysControlInfo infoDefault = bllSysCon.GetDefaultData();
            if (infoDefault.YearType != null)
            {
                cbYearType.SelectedValue = infoDefault.YearType.ToString();
                Session["yearSeq"] = cbYearType.SelectedValue;
                Session["yearSeq_Urgent"] = cbYearType.SelectedValue;
                
            }
            if (infoDefault.Year != null)
            {
                txtYear.Text = infoDefault.Year.ToString();
                Session["year"] = txtYear.Text;
                Session["year_Urgent"] = txtYear.Text;
            }
        }

        protected void LoadDataGrid()
        {
            RecevieItemBLL obj = new RecevieItemBLL();
            DataTable dt;
            dt = obj.Select();
            Grid.DataSource = dt;
            Grid.DataBind(); 

        }

        protected void LoadDataGrid_Urgent()
        {
            int YearTypeSeq = Convert.ToInt32(cbYearType.SelectedValue.ToString());
            int Year = Convert.ToInt32(txtYear.Text);
            Nullable<int> JobSiteSeq = (cbJobSite.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbJobSite.SelectedValue.ToString()) : null;
            Nullable<int> ProgressSeq = (cbProgress.SelectedIndex >= 0) ? (Nullable<int>)Convert.ToInt32(cbProgress.SelectedValue.ToString()) : null;
            string StatusSeq = (cbStatus.SelectedIndex >= 0) ? cbProgress.SelectedValue.ToString() : "";

            //IList<PrePurchaseRequestInfo> infoList = bllPPR.GetPrePurchaseRequestUrgent(YearTypeSeq, Year, JobSiteSeq, ProgressSeq, StatusSeq);
            //Grid_Urgent.DataSource = infoList;
            //Grid_Urgent.DataBind();
        }

        protected void LoadYearType()
        {
            IList<SysYearTypeInfo> infoList = bllYearType.GetSysYearType();
            cbYearType.DataSource = infoList;
            cbYearType.DataBind();
        }

        protected void LoadMMType()
        {
            RecevieItemBLL obj = new RecevieItemBLL();
            DataTable dt;
            dt = obj.SelectMMTypeInv();
            cbMMType.DataSource = dt;
            cbMMType.DataBind();

        }

        protected void LoadRecordStatus()
        {
            IList<SysRecordStatusInfo> infoList = bllStatus.GetSysRecordStatusTransaction();
            cbStatus.DataSource = infoList;
            cbStatus.DataBind();
        }

        public void LoadJobSiteTree()
        {
            IList<TreeInfo> info = bllTree.GetJobSiteTree();
            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");

            tree.DataSource = info;
            tree.DataBind();
        }

        protected void LoadProgress()
        {
            IList<SysProgressInfo> infoList = bllProgress.GetSysProgress();
            cbProgress.DataSource = infoList;
            cbProgress.DataBind();
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {

        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            //int[] data_value = new int[Grid.MasterTableView.GetSelectedItems().Length];
            //int item = 0;
            //foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
            //{
            //    data_value[item] = Convert.ToInt32(data.Cells[data.Cells.Count - 1].Text);
            //    item++;
            //}

            //for (int i = 0; i < data_value.Length; i++)
            //{
            //    PrePurchaseRequestInfo InfoPpr = new PrePurchaseRequestInfo();
            //    RecordInfo infoRecord = new RecordInfo();
            //    InfoPpr.Seq = data_value[i];

            //    infoRecord.Updated_by = 1; //UserID
            //    infoRecord.Updated_when = DateTime.Now;
            //    InfoPpr.InfoRecord = infoRecord;

            //    bllPPR.Delete(InfoPpr);
            //}
            LoadDataGrid();
        }

        protected void Delete_Urgent_Click(object sender, EventArgs e)
        {
            //int[] data_value = new int[Grid_Urgent.MasterTableView.GetSelectedItems().Length];
            //int item = 0;
            //foreach (GridDataItem data in Grid_Urgent.MasterTableView.GetSelectedItems())
            //{
            //    data_value[item] = Convert.ToInt32(data.Cells[data.Cells.Count - 1].Text);
            //    item++;
            //}

            //for (int i = 0; i < data_value.Length; i++)
            //{
            //    PrePurchaseRequestInfo InfoPpr = new PrePurchaseRequestInfo();
            //    RecordInfo infoRecord = new RecordInfo();
            //    InfoPpr.Seq = data_value[i];

            //    infoRecord.Updated_by = 1; //UserID
            //    infoRecord.Updated_when = DateTime.Now;
            //    InfoPpr.InfoRecord = infoRecord;

            //    bllPPR.Delete(InfoPpr);
            //}
            LoadDataGrid_Urgent();
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            //if (RadTabStrip1.SelectedTab.Value == "Normal")
            //{
            //    LoadDataGrid();
            //    Session["yearSeq"] = cbYearType.SelectedValue;
            //    Session["year"] = txtYear.Text;
            //}
            //else if (RadTabStrip1.SelectedTab.Value == "Urgent")
            //{
            //    LoadDataGrid_Urgent();
            //    Session["yearSeq_Urgent"] = cbYearType.SelectedValue;
            //    Session["year_Urgent"] = txtYear.Text;
            //}
        }

        protected void RadManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "CloseWindow")
            {
                LoadDataGrid();
            }
            else
            {
                LoadDataGrid_Urgent();
            }
        }

        protected void Grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_Urgent_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            LoadDataGrid_Urgent();
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
        //    if (RadTabStrip1.SelectedTab.Value == "Normal")
        //    {
        //        LoadDataGrid();
        //    }
        //    else if (RadTabStrip1.SelectedTab.Value == "Urgent")
        //    {
        //        LoadDataGrid_Urgent();
        //    }

        //    panelSearchForNormal.Visible = (RadTabStrip1.SelectedTab.Value == "Normal");
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }
    }
}
